package com.fzk.router;

import com.fzk.log.Logger;
import com.fzk.server.HttpResponseUtil;
import com.fzk.vo.HttpResult;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpUtil;

import java.util.List;
import java.util.Map;

/**
 * @author fzk
 * @datetime 2023-04-13 23:46:15
 */
@FunctionalInterface // 函数式接口
public interface SimpleApiHandler extends ApiHandler {
    @Override
    default void handle(MyCtx ctx, FullHttpRequest request, Map<String, List<String>> params) {
        try {
            HttpResult result = doHandle(ctx, request, params);
            HttpResponseUtil.json(ctx.getChannelCtx(), HttpUtil.isKeepAlive(request), result);
        } catch (Exception e) {
            e.printStackTrace();
            Logger.error(e.getMessage());
            HttpResponseUtil.json(ctx.getChannelCtx(), HttpUtil.isKeepAlive(request), new HttpResult(HttpResponseStatus.INTERNAL_SERVER_ERROR.code(), "服务异常: " + e.getMessage(), null));
        }
    }

    HttpResult doHandle(MyCtx ctx, FullHttpRequest request, Map<String, List<String>> params);
}
